package LS;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebFilter("/*")
public class MLoginCheckFilter implements Filter {

    public MLoginCheckFilter() {
    }

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse resp = (HttpServletResponse)response;
		HttpSession session  = req.getSession();
		String a = req.getRequestURI().toString().substring(10,11);
		
		if("m".equalsIgnoreCase(a)){
			if(session.getAttribute("level")!=null){
				if(!(session.getAttribute("level").equals("M"))){
					session.setAttribute("RETURN_URL",req.getRequestURL().toString());
					resp.sendRedirect("/LSsystem/index.do");
					return;
				}
			}	
			else{
				session.setAttribute("RETURN_URL",req.getRequestURL().toString());
				resp.sendRedirect("/LSsystem/index.do");
				return;
			}
		}
		else if("u".equalsIgnoreCase(a)){
			if(session.getAttribute("user")==null){
				session.setAttribute("RETURN_URL",req.getRequestURL().toString());
				resp.sendRedirect("/LSsystem/index.do");
			}
		}else if("t".equalsIgnoreCase(a)){
			if(session.getAttribute("level")!=null){
				if(!(session.getAttribute("level").equals("P"))){
					session.setAttribute("RETURN_URL",req.getRequestURL().toString());
					resp.sendRedirect("/LSsystem/index.do");
					return;
				}
			}	
			else{
				session.setAttribute("RETURN_URL",req.getRequestURL().toString());
				resp.sendRedirect("/LSsystem/index.do");
			}
		}else{
			
		}
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
